﻿using AutoMapper;
using NetTaste;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VM.Common.Result;
using VM.Entity.EntityDto.EnTry;
using VM.Entity.EntityDto.System.Code;
using VM.Entity.EntityMap.DataBase;
using VM.Entity.EntityMap.EnTry;
using VM.Entity.EntityMap.Menu;
using VM.Entity.EntityMap.System.Code;
using VM.Entity.EntityMap.Topic;
using VM.IDAL.System.Entry;

namespace VM.DAL.System.Entry
{
    public class EntryDAL : BaseDAL, IEntryDAL
    {

        protected ISqlSugarClient _Client { get; set; }
        protected IMapper _IMapper { get; set; }

        public EntryDAL(ISqlSugarClient client, IMapper iMapper) : base(client)
        {
            _Client = client;
            _IMapper = iMapper;
        }


        public int addEntry(sys_entry obj)
        {
            int Res = _Client.Insertable(obj).ExecuteCommand();
            return Res;
        }


        public PagingData<getCodeRuleListDto> getEntryTreeListDto(string Name, int UserId, int pageSize, int pageIndex)
        {

            var listRes = _Client.Queryable<sys_codeRule>()
                    .Includes(a => a.createUser)
                    .Includes(a => a.updateUser)
                    .WhereIF(Name != "" || Name != null, a => a.name.Contains(Name))
                    .Where(a => a.billStatus != 999)
                    ;

            PagingData<sys_codeRule> result = new PagingData<sys_codeRule>()
            {
                dataList = listRes.ToPageList(pageIndex, pageSize),
                pageIndex = pageIndex,
                pageSize = pageSize,
                recordCount = listRes.Count(),
            };

            PagingData<getCodeRuleListDto> resend = _IMapper.Map<PagingData<sys_codeRule>, PagingData<getCodeRuleListDto>>(result);
            return resend;

        }

        public int updateEntry(sys_entry obj)
        {
            int Res = _Client.Updateable<sys_codeRule>(obj)
                   .IgnoreColumns(ignoreAllNullColumns: true)
                   .ExecuteCommand();

            return Res;
        }



        public int deleteEntry(sys_entry obj)
        {
            throw new NotImplementedException();
        }

        List<getEntryTreeListDto> IEntryDAL.getEntryTreeListDto(string TypeName, int UserId)
        {
            List<getEntryTreeListDto> listRes = _Client.Queryable<getEntryTreeListDto>()//.ToList();
               .Where(a => a.typeId == 10005)  //写死的，数据字典类别-兴趣词条类别
               .Includes(a => a.children)
               .ToList();

            return listRes;
        }

        public int addUserEntryLink(List<sys_userEntryLink> obj)
        {
            int Res = _Client.Insertable(obj).ExecuteCommand();

            return Res;

        }
    }
}
